package cn.tangwei.order.controller;

import cn.tangwei.basic.utils.AjaxRusult;
import cn.tangwei.basic.utils.LoginContext;
import cn.tangwei.basic.utils.PageList;
import cn.tangwei.order.domain.ProductOrder;
import cn.tangwei.order.query.ProductOrderQuery;
import cn.tangwei.order.service.IProductOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/productOrder")
public class ProductOrderController {

    @Autowired
    private IProductOrderService productOrderService;

    /**
     * 购买服务下单接口
     * @param params
     * @param request
     * @return
     */
    @PostMapping("/submit")
    public AjaxRusult submit(@RequestBody Map<String,Object> params, HttpServletRequest request){
        try {
            String payData = productOrderService.submit(params, LoginContext.currentLoginInfo(request));
            System.out.println(payData);
            return AjaxRusult.me().setResultObj(payData);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxRusult.me().setSuccess(false).setMessage("下单失败！"+e.getMessage());
        }
    }

    @PostMapping("/admin")
    public PageList<ProductOrder> admin(@RequestBody ProductOrderQuery query, HttpServletRequest request){
        return productOrderService.admin(query, LoginContext.currentLoginInfo(request));
    }

    @GetMapping("/{id}")
    public ProductOrder getInfo(@PathVariable("id") Long id){
        return productOrderService.queryById(id);
    }

}
